<template>
  <div>
    <common-head v-show="showHead" title="商品列表"/>
    <!-- 排序规则 -->
    <div class="filter" :style="{top:navTop}">
      <div
        @click="orderBySale"
        :class="['sale', 'order', {
        active: order.by === 'sale'
      }]">
        <span>销量</span>
        <div :class="['up', 'angle', {
          active:  order.by === 'sale' && order.upDown === 0
        }]"></div>
        <div  :class="['down', 'angle', {
          active:  order.by === 'sale' && order.upDown === 1
        }]"></div>
      </div>
      <div
        @click="orderByPrice"
        :class="['price', 'order', {
        active: order.by === 'price'
      }]">
        <span>价格</span>
         <div :class="['up', 'angle', {
          active:  order.by === 'price' && order.upDown === 0
        }]"></div>
        <div  :class="['down', 'angle', {
          active:  order.by === 'price' && order.upDown === 1
        }]"></div>
      </div>
    </div>
    <!-- /排序规则 -->
    <div class="wrap">
       <div class="items">
      <div class="item" v-for="item in items" :key="item.id">
        <img :src="item.pic" alt="" class="item-left">
        <div class="item-info">
          <h3 class="name">{{item.name}}</h3>
          <p class="desc">
            {{ item.characteristic ? item.characteristic: '暂无描述' | filterDesc}}
          </p>
          <div class="item-action">
            <p class="price">
              {{ item.minPrice | currency }}
            </p>
            <van-icon class-prefix="icon" color="#f88" size="22" name="jia"></van-icon>
          </div>
        </div>
      </div>
    </div>
    </div>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { fetchItems } from '@api'
export default {
  data () {
    return {
      order: {
        by: 'sale', // sale 按照销量排序 price 按照价格排序
        upDown: 1 // 1降序 0 升序
      },
      showHead: true,
      navTop: '46px',
      items: []
    }
  },
  mounted () {
    this.scrollHead()
  },
  created () {
    this.fetchItems()
  },
  methods: {
    fetchItems () {
      fetchItems({
        categoryId: this.$route.query.cateId,
        orderBy: this.orderBy
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
      })
    },
    scrollHead () {
      // 滚动导航效果
      window.onscroll = () => {
        const sTop = document.documentElement.scrollTop || document.body.scrollTop
        if (sTop > 46) {
          this.showHead = false
          this.navTop = 0
        } else {
          this.showHead = true
          this.navTop = '46px'
        }
      }
    },
    orderBySale () {
      // 点击按销量排序
      // 此时 按照价格排序  点击按照销量 （默认按照销量降序）
      if (this.order.by === 'price') {
        this.order.by = 'sale'
        this.order.upDown = 1
      } else {
        // 已经按照销量排序 只需要 切换 升序还是降序即可
        if (this.order.upDown === 1) {
          this.order.upDown = 0
        } else {
          this.order.upDown = 1
        }
      }
    },
    orderByPrice () {
      // 点击按价格排序
      // 此时 按照销量排序  点击按照价格 （默认按照销量升序序）
      if (this.order.by === 'sale') {
        this.order.by = 'price'
        this.order.upDown = 0
      } else {
        // 已经按照价格排序 只需要 切换 升序还是降序即可
        if (this.order.upDown === 1) {
          this.order.upDown = 0
        } else {
          this.order.upDown = 1
        }
      }
    }
  },
  filters: {
    filterDesc (v, len = 10) {
      if (v.length > 10) {
        return v.substr(0, len) + '...'
      } else {
        return v
      }
    },
    currency (v, sign = '￥') {
      return sign + v
    }
  },
  watch: {
    order: {
      handler (val) {
        this.fetchItems()
      },
      deep: true
    }
  },
  computed: {
    orderBy () {
      if (this.order.by === 'sale' && this.order.upDown === 1) {
        return 'ordersDown'
      }
      if (this.order.by === 'sale' && this.order.upDown === 0) {
        return 'ordersUp'
      }
      if (this.order.by === 'price' && this.order.upDown === 0) {
        return 'priceUp'
      }
      if (this.order.by === 'price' && this.order.upDown === 1) {
        return 'priceDown'
      }
      return 'ordersDown'
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.filter{
  height: 40px;
  width: 100%;
  position: fixed;
  left:0;
  display: flex;
  align-items: center;
  justify-content: space-around;
  border-bottom: 1px solid #ccc;
  background-color: #fff;
  .order{
    font-size: 12px;
    color: #333;
    position: relative;
    &.active{
      color:#f66;
    }
    .angle{
      width:0;
      height: 0;
      border: 4px solid transparent;
      position: absolute;
      right: -10px;
      &.up{
        // 上尖角
        border-bottom-color: #333;
        top:0;
        &.active{
          border-bottom-color: #f66;
        }
      }
      &.down{
        // 下尖角
        border-top-color: #333;
        top:10px;
         &.active{
          border-top-color: #f66;
        }
      }
    }
  }
}
.wrap{
  padding-top: 97px;
  .items{
  .item{
    display: flex;
    &-left{
      width: 160px;
      height: 160px;
    }
    &-info{
      flex:1;
      padding:0 10px;
      box-sizing: border-box;
      color: #333;
      .name{
        font-size: 16px;
         color: #333;
         line-height: 30px;
      }
      .desc{
         font-size: 14px;
         color: #666;
      }
      .item-action{
        display: flex;
        margin-top: 10px;
        justify-content: space-between;
        align-items: center;
        p.price{
          color:#f88;
        }
      }
    }
  }
}
}
</style>
